using System;
using Gj.Galaxy;
using Gj.Galaxy.Logic;
using Gj.Services;
using UnityEngine;

namespace Gj
{
    public class INFO_TYPE
    {
        public const byte BASE = 0;
        public const byte STATUS = 1;
        public const byte ITEM = 2;
        public const byte OBJECT = 3;
    }

    public enum ServerKey : byte
    {
        Start
    }

    public class DataPlugin : GamePlugin
    {
        private int all;
        private int current;
        private BasePage Page;

        public void OnInit(BaseGameSpace space)
        {
            Page = space.UiSpace.Page;
            // 获取游戏数据
            space.UiSpace.Page.On(SystemEvent.DataSkill, o =>
            {
                current++;
                EngineService.single.SetSkills((object[])o);
                StatusService.single.dataProcess = current * 100 / all;
            });
            space.UiSpace.Page.On(SystemEvent.DataBuff, o =>
            {
                current++;
                EngineService.single.SetBuffs((object[])o);
                StatusService.single.dataProcess = current * 100 / all;
            });
            space.UiSpace.Page.On(SystemEvent.DataObject, o =>
            {
                current++;
                ControlService.single.SetObjects((object[])o);
                StatusService.single.dataProcess = current * 100 / all;
            });
            space.UiSpace.Page.On(SystemEvent.DataAttr, o =>
            {
                current++;
                AttrService.single.SetAttrs((object[])o);
                StatusService.single.dataProcess = current * 100 / all;
            });
            space.UiSpace.Page.On(SystemEvent.DataAttrGroup, o =>
            {
                current++;
                AttrService.single.SetGroups((object[])o);
                StatusService.single.dataProcess = current * 100 / all;
            });
        }

        public void OnDestroy()
        {
        }

        public GameService[] GetRequireService()
        {
            return new GameService[]
                { EngineService.single, AttrService.single, StatusService.single, DataService.single };
        }

        public void OnUpdate()
        {
            if (StatusService.single.dataProcess == 0 && StatusService.single.init)
            {
                all++;
                Page.Send(SystemMessage.GetDataSkill);
                all++;
                Page.Send(SystemMessage.GetDataBuff);
                all++;
                Page.Send(SystemMessage.GetDataObject);
                all++;
                Page.Send(SystemMessage.GetDataAttr);
                all++;
                Page.Send(SystemMessage.GetDataAttrGroup);
                StatusService.single.dataProcess = 1;
                return;
            }

            if (!StatusService.single.dataEnd && StatusService.single.dataProcess > 0 && all == current)
            {
                StatusService.single.dataEnd = true;
            }
        }

        public void LateUpdate()
        {
        }

        public void OnProcessRoom(object[] data)
        {
            // 处理进程
            foreach (object[] detail in data)
            {
                var key = (InfoKey)detail[(byte)DetailKey.Key];
                switch (key)
                {
                    case InfoKey.ServerKey:
                        var serverData = (object[])detail[(byte)DetailKey.Value];
                        var startTime = Convert.ToInt32(serverData[(byte)ServerKey.Start]);
                        // todo 服务器启动时间, 传入ui倒计时
                        Page.Send(SystemMessage.StartTime, (int)Client.FixTimestamp(startTime));
                        break;
                    case InfoKey.IdentityKey:
                        // todo 身份
                        DataService.single.PlayerIdentity = Convert.ToInt32(detail[(byte)DetailKey.Value]);
                        break;
                    case InfoKey.ObjectDataKey:
                        var itemData = (object[])detail[(byte)DetailKey.Value];
                        for (var i = 0; i < itemData.Length; i += 2)
                            DataService.single.SetIndex(INFO_TYPE.ITEM, Convert.ToInt32(itemData[i + 1]),
                                Convert.ToInt32(itemData[i]));

                        break;
                    case InfoKey.ActorDataKey:
                        var aiData = (object[])detail[(byte)DetailKey.Value];
                        for (var i = 0; i < aiData.Length; i += 2)
                            DataService.single.SetIndex(INFO_TYPE.OBJECT, Convert.ToInt32(aiData[i + 1]),
                                Convert.ToInt32(aiData[i]));

                        break;
                }
            }
        }

        public void OnFinish(object result)
        {
        }

        public void OnSwitchArea()
        {
        }

        public void OnInstance(int targetIndex, byte relation, GamePlayer player, Vector3 position, bool isLocal,
            NetworkEsse master, Action<InitSyncListener> cb)
        {
        }

        public void OnDestroy(NetworkEsse instance, GamePlayer player)
        {
        }

        public void OnBroadcast(GamePlayer player, byte code, Vector3 position, StreamReader reader)
        {
        }

        public void OnEnterRoom(bool first)
        {
        }

        public void OnPlayerJoin(GamePlayer player)
        {
        }

        public void OnPlayerLeave(GamePlayer player)
        {
        }

        public void OnPlayerRejoin(GamePlayer player)
        {
        }

        public void OnFailConnect()
        {
        }

        public void OnReady()
        {
        }

        public void OnReadyPlayer(GamePlayer player)
        {
        }

        public void OnStart()
        {
        }

        public void OnReadyAll()
        {
        }

        public void OnInitGame()
        {
        }

        public void OnTimeMove(int startTime, int endTime, StreamReader reader, Vector3[] positions, int index,
            Vector3 middlePosition)
        {
        }

        public void OnTimeProcess(int startTime, int endTime, StreamReader reader)
        {
        }
    }
}